var docClientHeight = document.documentElement.clientHeight;
jQuery(function($){
var subTarget = $('#categories-subs');
var baseHeight = $('#categories').offset().top - 38;
var docScrollTop = $(document).scrollTop();	
$('#categories').find('li:not(:.node):not(:.box-bottom)').each(function(i){
  var parent = $(this);
  parent.hover(
    function(){
	$('#categories li').filter(':not(:eq('+i+'))').removeClass('current');
	  parent.addClass('current');
	  var parentTop = parent.offset().top;	   
	  $('#sec-cate-zone').find('.sec-categories').removeClass('cate-active');
	  $('#sec-cate-zone').find('.sec-categories').each(function(j){
	     if($(this).attr('rel') == parent.find('span').html())
		 {
		   subTarget.css({left:'171px'});
		   if(parentTop - docScrollTop + $(this).height() > docClientHeight)
		   {
		     var _top = (docClientHeight - $(this).height() - baseHeight + docScrollTop);
			 if(_top + $(this).height() < parentTop -  parent.height() - baseHeight)
			 {
				subTarget.css({top: _top + 20 +'px'});
			 }
			 else
			 {
				subTarget.css({top: _top +'px'});
			 }
		   }
		   else
		   {		   
		     subTarget.css({top: parent.offset().top - $('#categories').offset().top - 20 +'px'});
		   }		   
		   $(this).addClass('cate-active');		   
		 }		
	  })
	},
	
	function(){
	$("#categories").bind("mouseleave",function(event){
		parent.removeClass('current');
		$('#sec-cate-zone').find('.sec-categories').each(function(j){
	     if($(this).attr('rel') == parent.find('span').html())
		 {
		   $(this).removeClass('cate-active');
		 }		
		});}); 	  
	}
  )
})
});